package com.bit.book.service;

import com.bit.book.entity.BookInfo;
import com.bit.book.dao.BookDao;
import com.bit.book.entity.PageRequest;
import com.bit.book.entity.PageResult;
import com.bit.book.enums.BookStatus;
import com.bit.book.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {

    private static final String SESSSION_KEY = "session_key";
    @Autowired
    private BookDao bookDao;

    @Autowired
    private BookMapper bookMapper;

    public List<BookInfo> getList(){

        List<BookInfo> bookInfos = bookDao.mockBookData();
        for (BookInfo bookInfo: bookInfos){
            if (bookInfo.getStatus()==1){
                bookInfo.setStatusCN("可借阅");
            }else {
                bookInfo.setStatusCN("不可借阅");
            }
        }
        return bookInfos;

    }

    public Integer addBook(BookInfo bookInfo) {
        return bookMapper.insertBook(bookInfo);
    }

    public PageResult getListByPage(PageRequest pageRequest) {
        //count
        Integer count = bookMapper.count();
        //list
        List<BookInfo> bookInfos = bookMapper.selectList(pageRequest);
        for (BookInfo bookInfo: bookInfos){
            bookInfo.setStatusCN(BookStatus.getDescByCode(bookInfo.getStatus()).getDesc());
        }
        return new PageResult<>(count, bookInfos, pageRequest);
    }

    public BookInfo queryBookById(Integer bookId) {
        return bookMapper.selectById(bookId);
    }

    public Integer updateBookById(BookInfo bookInfo) {
        return bookMapper.updateBookById(bookInfo);
    }

    public Integer deleteBook(Integer bookId) {
        BookInfo bookInfo = new BookInfo();
        bookInfo.setStatus(BookStatus.DELETED.getCode());
        bookInfo.setId(bookId);
        return bookMapper.updateBookById(bookInfo);
    }

    public Integer batchDeleteBook(List<Integer> ids) {
        return bookMapper.batchDeleteBook(ids);
    }
}
